package com.hboxs.asl.controller.app.member;

import com.hboxs.asl.JsonMessage;
import com.hboxs.asl.Page;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.controller.app.BaseController;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.service.MemberService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Set;

/**
 * 我的关注
 */
@Controller(value = "appFollowController")
@RequestMapping(value = "/app/m/follow")
public class FollowController extends BaseController {

    @Resource(name = "memberServiceImpl")
    private MemberService memberService;

    /**
     * 我关注的老师
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage list (Pageable pageable) {
        try {
            Member member = memberService.getCurrentByToken();
            if(member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            Page<Member> page = memberService.findPageTeacher(pageable, member);

            return JsonMessage.success(
                    "totalPage", page.getTotalPages(),
                    "pageNumber", page.getPageNumber(),
                    "pageSize", page.getPageSize(),
                    "total", page.getTotal(),
                    "content", JsonMessage.optimiseObj(page.getContent(), "id", "avator", "name", "introduction", "tags"));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 关注/取消关注
     * @param id
     * @return
     */
    @RequestMapping(value = "/change", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage change(Long id) {
        try {
            Member teacher = memberService.find(id);
            if(teacher == null) {
                return JsonMessage.error("该用户不存在");
            }
            Member currentMember = memberService.getCurrentByToken();
            if(currentMember == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            if (teacher.getId().equals(currentMember.getId())) {
                return JsonMessage.error("不能关注自己");
            }
            boolean follow = true;
            //我关注的老师
            Set<Member> teacherSet = currentMember.getFavorAnchors();
            if(teacherSet.contains(teacher)) {
                teacherSet.remove(teacher);
                follow = false;
            } else {
                teacherSet.add(teacher);
                follow = true;
            }
            memberService.update(currentMember);
            return JsonMessage.success(follow);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

}
